package com.dangdi.show.service;

import com.aliyuncs.exceptions.ClientException;
import com.dangdi.show.dao.ManagerDao;
import com.dangdi.show.dao.SmsDemo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.swing.table.TableStringConverter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Transactional
public class ManagerService {
    @Resource
    ManagerDao managerDao;

    //获取所有管理员
    public List<Map<String, Object>> FindAllManagerList(int page, int limit) {
        return managerDao.FindAllManagerList(page, limit);
    }

    public int FindAllManagerListCount() {
        return managerDao.FindAllManagerListCount();
    }

    //管理员登录
    public Map<String, Object> ManagerLogin(String manager_tel, String manager_password) {
        return managerDao.ManagerLogin(manager_tel, manager_password);
    }

    public void lahei(int manager_id, int status) {
        managerDao.lahei(manager_id, status);
    }

    public void shanchu(int manager_id) {
        managerDao.shanchu(manager_id);
    }

    public Map<String, Object> FindManagerBymanagerId(int manager_id) {
        return managerDao.FindManagerBymanagerId(manager_id);
    }

    public List<Map<String, Object>> FindManagerRoleList(@Param("manager_id") int manager_id) {
        return managerDao.FindManagerRoleList(manager_id);
    }

    public void UpdateManagerPhoto(int manager_id, String manager_photo) {
        managerDao.UpdateManagerPhoto(manager_id, manager_photo);
    }

    public void updateManagerInfo(String manager_name, String manager_tel, String manager_password, String manager_postion, String manager_address, String manager_sex, int manager_id) {
        managerDao.updateManagerInfo(manager_name, manager_tel, manager_password, manager_postion, manager_address, manager_sex, manager_id);
    }

    public void shanchuRoleByRoleId(int role_id) {
        managerDao.shanchuRoleByRoleId(role_id);
    }

    public List<Map<String, Object>> FindallRoleList() {
        return managerDao.FindallRoleList();
    }

    public void addRoleToObjectManager(int manager_id, String r_name) {
        managerDao.addRoleToObjectManager(manager_id, r_name);
    }

    public List<Map<String, Object>> searchManager(int i, int i1, String title) {
        if (isChinese(title)) {
            return managerDao.searchManagerChinese(i, i1, "%" + title + "%");
        } else {
            return managerDao.searchManager(i, i1, "%" + title + "%");
        }
    }

    public int searchManagerCount(String title) {
        if (isChinese(title)) {
            return managerDao.searchManagerChineseCount("%" + title + "%");
        } else {
            return searchManagerCount("%" + title + "%");
        }
    }

    public static boolean isChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find())
            flg = true;
        return flg;
    }

    public void updateThisPassword(String newPassword1, int manager_id) {
        managerDao.updateThisPassword(newPassword1, manager_id);
    }

    public void updateThisPhoneNum(String newPhone, int manager_id) {
        managerDao.updateThisPhoneNum(newPhone, manager_id);
    }

    public void insertManager(String manager_name, String manager_tel, String manager_password, String manager_postion, String manager_address, String manager_photo, String manager_sex) {
        managerDao.insertManager(manager_name, manager_tel, manager_password, manager_postion, manager_address, manager_photo, manager_sex);
    }

    public Map<String, Object> FindRoleByR_name(String r_name) {
        return managerDao.FindRoleByR_name(r_name);
    }

    public void InsertRole(String r_name, String r_explain) {
        managerDao.InsertRole(r_name, r_explain);
    }

    public void shanchuRoleByRId(int r_id) {
        managerDao.shanchuRoleByRId(r_id);
    }

    public List<Map<String, Object>> FindHaverPermissionListByR_id(String r_name, int page, int limit) {
        return managerDao.FindHaverPermissionListByR_id(r_name, page, limit);
    }


    public void shanchuPermissionByPermissionId(String p_name, String objectRid) {
        managerDao.shanchuPermissionByPermissionId(p_name, objectRid);
    }

    public List<Map<String, Object>> searchPermission(String title, String objectRid, int page, int limit) {
        return managerDao.searchPermission("%" + title + "%", objectRid, page, limit);
    }

    public List<Map<String, Object>> FindNotHaverPermissionListByR_id(String r_name, int page, int limit) {
        return managerDao.FindNotHaverPermissionListByR_id(r_name, page, limit);
    }

    public void addPermissionToRole(String p_name, String r_name) {
        managerDao.addPermissionToRole(p_name, r_name);
    }

    public List<Map<String, Object>> searchPermission2(String title, String objectRid, int page, int limit) {
        return managerDao.searchPermission2("%" + title + "%", objectRid, page, limit);
    }

    public void insertRecord(String record_type, String manager_name, String record_object, String record_before, String record_after, String record_object_name, String record_remarks) {
        managerDao.insertRecord(record_type, manager_name, record_object, record_before, record_after, record_object_name, record_remarks);
    }

    public Map<String, Object> FindRByRoleId(int role_id) {
        return managerDao.FindRByRoleId(role_id);
    }

    public Map<String, Object> FindRByRId(int r_id) {
        System.out.println(r_id);
        return managerDao.FindRByRId(r_id);
    }

    public Map<String, Object> FindPByPname(String p_name) {
        return managerDao.FindPByPname(p_name);
    }

    public Map<String, Object> FindRByRname(String r_name) {
        return managerDao.FindRByRname(r_name);
    }

    public List<Map<String, Object>> FindAllRecordList(int page, int limit) {
        return managerDao.FindAllRecordList(page, limit);
    }

    public int FindAllRecordListCount() {
        return managerDao.FindAllRecordListCount();
    }

    public List<Map<String, Object>> searchRecord(int i, int i1, String title, String adate, String ddate) {
        return managerDao.searchRecord(i, i1, "%" + title + "%", adate, ddate);
    }

    public List<Map<String, Object>> searchRecordNoDate(int i, int limit, String title) {
        return managerDao.searchRecordNoDate(i, limit, "%" + title + "%");
    }

    public List<Map<String, Object>> findManagerByRName(String R_name, int page, int limit) {
        return managerDao.findManagerByRName(R_name, page, limit);
    }

    public int quxiaoManagerRole(@Param("manager_id") int manager_id, @Param("role_name") String role_name) {
        return managerDao.quxiaoManagerRole(manager_id, role_name);
    }

    public List<Map<String, Object>> getProvinceList(int page, int limit) {
        return managerDao.getProvinceList(page, limit);
    }

    public List<Map<String, Object>> getCityList(int page, int limit) {
        return managerDao.getCityList(page, limit);
    }

    public List<Map<String, Object>> getAreaList(int page, int limit) {
        return managerDao.getAreaList(page, limit);
    }

    public List<Map<String, Object>> searchShengfen(String shengfen, int page, int limit) {
        return managerDao.searchShengfen("%" + shengfen + "%", page, limit);
    }

    public List<Map<String, Object>> searchChengshi(String city, int page, int limit) {
        return managerDao.searchChengshi("%" + city + "%", page, limit);
    }

    public List<Map<String, Object>> searchArea(String area, int page, int limit) {
        return managerDao.searchArea("%" + area + "%", page, limit);
    }

    public int shanchuShengfen(int province_id) {
        return managerDao.shanchuShengfen(province_id);
    }

    public int shanchuChengshi(int city_id) {
        return managerDao.shanchuChengshi(city_id);
    }

    public int shanchuQuyu(int area_id) {
        return managerDao.shanchuQuyu(area_id);
    }

    public int addShengfen(String shengfen) {
        return managerDao.addShengfen(shengfen);
    }

    public int check_shengfen(String shengfen) {
        return managerDao.check_shengfen(shengfen);
    }

    public List<Map<String, Object>> filterChengshi(int province_id, int page, int limit) {
        return managerDao.filterChengshi(province_id, page, limit);
    }

    public int check_city(@Param("city") String city) {
        return managerDao.check_city(city);
    }

    public int addCity(int province_id, String city_name) {
        return managerDao.addCity(province_id, city_name);
    }

    public List<Map<String, Object>> filterArea(int city_id, int page, int limit) {
        return managerDao.filterArea(city_id, page, limit);
    }

    public int check_area(String area) {
        return managerDao.check_area(area);
    }

    public int addArea(int city_id, String area_name) {
        return managerDao.addArea(city_id, area_name);
    }

    public int delete_lunbo_img(int index_side_id) {
        int paper = (int) managerDao.findLunboImgById(index_side_id).get("index_side_paper");
        int row = managerDao.delete_lunbo_img(index_side_id);
        int row2 = managerDao.delete_lunbo_img2(paper);
        return row;
    }

    public int upload_index_lunbo_img(String index_side_img_path, int index_side_id) {
        if (index_side_id > 0) {
            int paper = (int) managerDao.findLunboImgById(index_side_id).get("index_side_paper");
            return managerDao.update_index_lunbo_img(index_side_img_path, paper);
        }
        return managerDao.upload_index_lunbo_img(index_side_img_path, managerDao.index_lunbo_count() + 1);
    }

    public int upload_index_haibao_img(String index_side_haibao_path) {
        managerDao.delete_haibao_tu();
        return managerDao.upload_index_haibao_img(index_side_haibao_path);
    }

    public String getIndexHaiBaoTu() {
        return managerDao.getIndexHaiBaoTu();
    }

    public Map<String, Object> getIndexHuodong() {
        return managerDao.getIndexHuodong();
    }

    public int upload_index_huodong_img(String index_huodong_img, String index_huodong_title) {
        return managerDao.upload_index_huodong_img(index_huodong_img, index_huodong_title);
    }

    public int upload_index_huodong_img2(@Param("index_huodong_img") String index_haibao_img, @Param("index_huodong_title") String index_huodong_title) {
        return managerDao.upload_index_huodong_img2(index_haibao_img, index_huodong_title);
    }

    public List<Map<String, Object>> getPublishList(@Param("page") int page, @Param("limit") int limit) {
        return managerDao.getPublishList(page, limit);
    }

    public List<Map<String, Object>> getScenehList(@Param("page") int page, @Param("limit") int limit) {
        return managerDao.getSceneList(page, limit);
    }

    public int getPublishListCount() {
        return managerDao.getPublishListCount();
    }

    public int getSceneListCount() {
        return managerDao.getSceneListCount();
    }

    public int deletePublishByPd_id(@Param("pd_id") int pd_id) {
        return managerDao.deletePublishByPd_id(pd_id);
    }

    public int deleteScene(@Param("pd_id") int pd_id) {
        return managerDao.deleteScenePd_id(pd_id);
    }

    public Map<String, Object> getPublishInfo(@Param("pd_id") int pd_id) {
        return managerDao.getPublishInfo(pd_id);
    }

    public Map<String, Object> getSceneInfo(@Param("pd_id") int pd_id) {
        return managerDao.getSceneInfo(pd_id);
    }

    public int updatePublishInfo(@Param("pd_id") int pd_id, @Param("pd_title") String pd_title, @Param("pd_content") String pd_content) {
        return managerDao.updatePublishInfo(pd_id, pd_title, pd_content);
    }

    public int updateSceneInfo(@Param("pd_id") int pd_id, @Param("pd_title") String pd_title, @Param("pd_content") String pd_content) {
        return managerDao.updateSceneInfo(pd_id, pd_title, pd_content);
    }

    public int updatePublishImg(@Param("pd_img") String pd_img, @Param("pd_id") int pd_id) {
        return managerDao.updatePublishImg(pd_img, pd_id);
    }

    public int updateSceneImg(@Param("pd_img") String pd_img, @Param("pd_id") int pd_id) {
        return managerDao.updateSceneImg(pd_img, pd_id);
    }

    public int addPublish(@Param("pd_title") String pd_title, @Param("pd_content") String pd_content, @Param("pd_image") String pd_image, @Param("manager_id") int manager_id) {
        return managerDao.addPublish(pd_title, pd_content, pd_image, manager_id);
    }

    public int addScene(@Param("pd_title") String pd_title, @Param("pd_content") String pd_content, @Param("pd_image") String pd_image, @Param("manager_id") int manager_id) {
        return managerDao.addScene(pd_title, pd_content, pd_image, manager_id);
    }

    public List<Map<String, Object>> searchPublish(@Param("pd_title") String pd_title, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.searchPublish("%" + pd_title + "%", page, limit);
    }

    public List<Map<String, Object>> searchScene(@Param("pd_title") String pd_title, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.searchScene("%" + pd_title + "%", page, limit);
    }

    public int searchPublishCount(@Param("pd_title") String pd_title) {
        return managerDao.searchPublishCount("%" + pd_title + "%");
    }

    public int searchSceneCount(@Param("pd_title") String pd_title) {
        return managerDao.searchSceneCount("%" + pd_title + "%");
    }

    public List<Map<String, Object>> getFeeScaleByPd_id(@Param("pd_id") int pd_id, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.getFeeScaleByPd_id(pd_id, page, limit);
    }

    public List<Map<String, Object>> searchFeeScale(@Param("pd_id") int pd_id, @Param("title") String title, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.searchFeeScale(pd_id, "%" + title + "%", page, limit);
    }

    public int add_feeScale(@Param("pd_id") int pd_id, @Param("publish_time") String publish_time, @Param("publish_price") float publish_price) {
        return managerDao.add_feeScale(pd_id, publish_time, publish_price);
    }

    public int deleteFeaScale(@Param("feeScale_id") int feeScale_id) {
        return managerDao.deleteFeaScale(feeScale_id);
    }

    public List<Map<String, Object>> getActorList(@Param("page") int page, @Param("limit") int limit) {
        return managerDao.getActorList(page, limit);
    }

    public int getActorListCount() {
        return managerDao.getActorListCount();
    }

    public int searchActorCount(@Param("title") String title) {
        System.out.println(managerDao.searchActorCount(title));
        return managerDao.searchActorCount("%" + title + "%");
    }

    public List<Map<String, Object>> searchActor(@Param("title") String title, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.searchActor("%" + title + "%", page, limit);
    }

    public Map<String, Object> getActorInfo(@Param("actor_id") int actor_id) {
        return managerDao.getActorInfo(actor_id);
    }

    public int update_actor(Map actor) {
        return managerDao.update_actor(actor);
    }

    public int add_actor(Map<String, Object> actor) {
        return managerDao.add_actor(actor);
    }

    public int updateActorImg(@Param("actor_img") String actor_img, @Param("actor_id") int actor_id) {
        return managerDao.updateActorImg(actor_img, actor_id);
    }

    public int deleteActor(@Param("actor_id") int actor_id) {
        return managerDao.deleteActor(actor_id);
    }

    public List<Map<String, Object>> getActorListByPublish_id(@Param("publish_id") int pd_id, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.getActorListByPublish_id(pd_id, page, limit);
    }

    public int getActorListByPublish_idCount() {
        return managerDao.getActorListByPublish_idCount();
    }

    public List<Map<String, Object>> searchGetActorListByPublish_id(@Param("title") String title, @Param("publish_id") int pd_id, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.searchGetActorListByPublish_id("%" + title + "%", pd_id, page, limit);
    }

    public int cancelActorOfPublish(@Param("publish_id") int publish_id, @Param("actor_id") int actor_id) {
        int row = managerDao.cancelActorOfPublish(publish_id, actor_id);
        if (row == 0) {
            row = managerDao.setActorPublish(actor_id, publish_id);
        }
        return row;
    }

    public Map<String, Object> getProperties(@Param("properties_name") String properties_name) {
        return managerDao.getProperties(properties_name);
    }

    public List<Map<String, Object>> getnewOderList(@Param("page") int page, @Param("limit") int limit) {
        return managerDao.getnewOderList(page, limit);
    }

    public int getnewOderListCount() {
        return managerDao.getnewOderListCount();
    }

    public List<Map<String, Object>> getnewOderList2(@Param("page") int page, @Param("limit") int limit) {
        return managerDao.getnewOderList2(page, limit);
    }

    public int getnewOderList2Count() {
        return managerDao.getnewOderList2Count();
    }

    public List<Map<String, Object>> getnewOderList3(@Param("page") int page, @Param("limit") int limit) {
        return managerDao.getnewOderList3(page, limit);
    }

    public int getnewOderList3Count() {
        return managerDao.getnewOderList3Count();
    }

    public List<Map<String, Object>> getnewOderList4(@Param("page") int page, @Param("limit") int limit) {
        return managerDao.getnewOderList4(page, limit);
    }

    public int getnewOderList4Count() {
        return managerDao.getnewOderList4Count();
    }

    public List<Map<String, Object>> getActorListByOrder_id(@Param("order_id") int order_id) {
        return managerDao.getActorListByOrder_id(order_id);
    }

    public int cancelActorByFeeScale_id(@Param("feeScale_id") int feeScale_id) {
        return managerDao.cancelActorByFeeScale_id(feeScale_id);
    }

    public List<Map<String, Object>> getActorListSetOrderByPublish_id(@Param("order_feeScale_id") int order_feeScale_id, @Param("publish_id") int publish_id, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.getActorListSetOrderByPublish_id(order_feeScale_id, publish_id, page, limit);
    }

    public int orderSetActorByFeeScale_id(@Param("actor_id") int actor_id, @Param("order_feeScale_id") int order_feeScale_id) {
        return managerDao.orderSetActorByFeeScale_id(actor_id, order_feeScale_id);
    }

    public List<Map<String, Object>> searchOrderSetActorList(@Param("title") String title, @Param("publish_id") int publish_id, @Param("page") int page, @Param("limit") int limit) {
        return managerDao.searchOrderSetActorList("%" + title + "%", publish_id, page, limit);
    }

    public List<Map<String, Object>> getActorForFeeScaleByOrder_id(@Param("order_id") int order_id) {
        return managerDao.getActorForFeeScaleByOrder_id(order_id);
    }

    public int cancelActorToOrder(@Param("order_feeScale_actor_id") int order_feeScale_id, @Param("actor_id") int actor_id) {
        return managerDao.cancelActorToOrder(order_feeScale_id, actor_id);
    }

    public int updateOrderStatusByOrder_id(@Param("order_status") int order_status, @Param("order_id") int order_id) throws ClientException, InterruptedException {
        if (managerDao.updateOrderStatusByOrder_id(order_status, order_id) == 1) {
            if (order_status == 2) {
                Map serviceTel = new HashMap();
                serviceTel.put("manager_tel", "13820688282");
                serviceTel.put("order_number", managerDao.getActorListByOrder_id(order_id).get(0).get("order_number").toString());
                //发给客户
                SmsDemo.sendSms(managerDao.getUser_telByOrder_id(order_id).get("user_tel").toString(), serviceTel, SmsDemo.MSG_TO_USER_ORDER_OK);
                //发给演员
                List<Map<String, Object>> actorList = managerDao.getOrderActorListByOrder_id(order_id);
//            serviceTel.clear();
                List<Map<String, Object>> publishList = managerDao.getPublishListByOrder_id(order_id);
                String str = "";
                for (int i = 0; i < publishList.size(); i++) {
                    str += publishList.get(i).get("pd_title").toString() + "-";
                    str += publishList.get(i).get("publish_time").toString();
                    if (i != publishList.size() - 1) {
                        str += ",";
                    }
                }
                serviceTel.put("show_time", managerDao.getUser_telByOrder_id(order_id).get("sd") + " " + managerDao.getUser_telByOrder_id(order_id).get("st"));
                serviceTel.put("order_feeScale_list", str);
                serviceTel.put("user_address", managerDao.getUser_telByOrder_id(order_id).get("user_sheng").toString() + managerDao.getUser_telByOrder_id(order_id).get("user_shi") + managerDao.getUser_telByOrder_id(order_id).get("user_qu") + managerDao.getUser_telByOrder_id(order_id).get("user_add"));
                for (int i = 0; i < actorList.size(); i++) {
                    serviceTel.put("actor_name", actorList.get(i).get("actor_name").toString());
                    SmsDemo.sendSms(actorList.get(i).get("actor_tel").toString(), serviceTel, SmsDemo.MSG_TO_ACTOR_ORDER_INFO);
                }
                serviceTel.clear();
                managerDao.updateOrderResponseTimeByOrder_id(order_id);
            } else if (order_status == 3) {
                managerDao.updateOrderDoneTimeByOrder_id(order_id);
            }

        } else {
            return -1;
        }
        return 1;
    }

    public List<Map<String, Object>> getOrder_publish_count() {
        return managerDao.getOrder_publish_count();
    }

    public List<Map<String, Object>> getAllPublish_order_count() {
        return managerDao.getAllPublish_order_count();
    }

    public int getOrderCountByYearAndMonth(@Param("yearMonth") String yearMonth) {
        return managerDao.getOrderCountByYearAndMonth(yearMonth);
    }

    public int deleteOrder_feeScaleByOrder_feeScale_id(int order_feeScale_id) {
        return managerDao.deleteOrder_feeScaleByOrder_feeScale_id(order_feeScale_id);
    }

    public List<Map<String, Object>> searchOderList(@Param("title") String title, @Param("page") int page, @Param("limit") int limit, @Param("order_status") int order_status) {
        return managerDao.searchOderList("%" + title + "%", page, limit, order_status);
    }

    public int searchOderListCount(@Param("title") String title, @Param("order_status") int order_status) {
        return managerDao.searchOderListCount(title, order_status);
    }

    public List<Map<String, Object>> getActorLishiOrder(@Param("page") int page, @Param("limit") int limit, @Param("actor_id") int actor_id) {
        return managerDao.getActorLishiOrder(page, limit, actor_id);
    }

    public int getActorLishiOrderCount(@Param("actor_id") int actor_id) {
        return managerDao.getActorLishiOrderCount(actor_id);
    }

    public List<Map<String, Object>> searchActorOderList(@Param("title") String title, @Param("page") int page, @Param("limit") int limit, @Param("actor_id") int actor_id) {
        return managerDao.searchActorOderList("%" + title + "%", page, limit, actor_id);
    }

    public List<Map<String, Object>> getSendMessageManagerList() {
        return managerDao.getSendMessageManagerList();
    }

    public List<Map<String, Object>> FindAllManagerList2(@Param("page") int page, @Param("limit") int limit) {
        return managerDao.FindAllManagerList2(page, limit);
    }

    public int FindAllManagerList2Count() {
        return managerDao.FindAllManagerList2Count();
    }

    public List<Map<String, Object>> searchManager2(int i, int i1, String title) {
        if (isChinese(title)) {
            return managerDao.searchManagerChinese2(i, i1, "%" + title + "%");
        } else {
            return managerDao.searchManager2(i, i1, "%" + title + "%");
        }
    }


    public int searchManagerCount2(String title) {
        if (isChinese(title)) {
            return managerDao.searchManagerChinese2Count2("%" + title + "%");
        } else {
            return managerDao.searchManager2Count2("%" + title + "%");
        }
    }

    public int deleteSendMessageManager(@Param("newOderMsgTel_manager_id") int newOderMsgTel_manager_id) {
        return managerDao.deleteSendMessageManager(newOderMsgTel_manager_id);
    }

    public int addSendMessageManager(@Param("manager_id") int manager_id) {
        System.out.println("manager_id=" + manager_id);
        return managerDao.addSendMessageManager(manager_id);
    }
}
